//
//  PayInterface.m
//  DaJiang
//
//  Created by sally on 16/12/11.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "PayInterface.h"

@implementation PayInterface

- (id)init {
  self = [super initWithPath:@"/pay"];
  if (self) {
  }
  return self;
}

//支付宝预支付
- (void)payAlipayPrepayWithOrderId:(NSString *)orderId
                           subject:(NSString *)subject
                          totalFee:(NSString *)totalFee
                        completion:(completion_t)completion
                             error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"alipay/prepay.json");
  UserModel *userModel = [[CacheManager shareInstance] getLastLoginAccount];

  NSDictionary *parameters = @{ @"uid" : @(userModel.uid),
                                @"order_id" : orderId,
                                @"subject" : subject,
                                @"total_fee" : totalFee,
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

@end
